package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"shop/models"
	"shop/shopRpc/internal/svc"
	"shop/shopRpc/shopRpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRealLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRealLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRealLogic {
	return &UserRealLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserRealLogic) UserReal(in *shopRpc.UserRealRequest) (*shopRpc.UserRealResponse, error) {
	// todo: add your logic here and delete this line
	err := models.DB.Transaction(func(tx *gorm.DB) error {
		var userReal models.UsersReals
		userReal.IDCard = in.IdCard
		userReal.RealName = in.RealName

		err := userReal.UpdateReal(models.DB, in.UserId)
		if err != nil {
			return errors.New("实名信息更新失败")
		}

		var user models.User
		user.IsReal = 1
		err = user.UpdateRealStatus(models.DB, in.UserId)
		if err != nil {
			return errors.New("实名状态更新失败")
		}
		return nil
	})
	if err != nil {
		return nil, errors.New("实名认证失败")
	}

	return &shopRpc.UserRealResponse{
		UserId: in.UserId,
	}, nil
}
